//
//  SortTableViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/19.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "SortTableViewCell.h"

@interface SortTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) BaseView *viewBackGround; /**< 背景 */
@property (nonatomic, retain) SortLayout *sortLayout;   /**< layout */
@property (nonatomic, retain) BaseCollectionView *collectionView;  /**< collection */

@property (nonatomic, retain) BaseLabel *labelHeader;  /**< 头部文字 */
@property (nonatomic, retain) BaseLabel *labelLine;  /**< 头部线 */
@end

@implementation SortTableViewCell

- (void)dealloc
{
    [_viewBackGround release];
    [_sortLayout release];
    [_collectionView release];
    [_labelHeader release];
    [_labelLine release];
    [_arrSort release];
    [super dealloc];
}

#pragma mark - ------------ tableview初始化 ---------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     
        /** 夜间 */
        self.contentView.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00],[UIColor lightGrayColor]);
        [self createCollectionView];
     
    }
    return self;

}

#pragma mark - ------------ 创建collectionView ---------------
- (void)createCollectionView
{

    //创建背景白色view
    self.viewBackGround = [[BaseView alloc]init];
    [self.contentView addSubview:_viewBackGround];
    
    self.sortLayout = [[SortLayout alloc]init];
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:self.viewBackGround.bounds collectionViewLayout:_sortLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    [self.viewBackGround addSubview:_collectionView];
    
    //注册头部cell 和cell
    [_collectionView registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    
   [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [_sortLayout release];
    [_collectionView release];
    [_viewBackGround release];

}

#pragma mark - ------------ 必须写的 赋大小 ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _viewBackGround.translatesAutoresizingMaskIntoConstraints = NO;
    [_viewBackGround mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.right.bottom.mas_equalTo(self.contentView).offset(0);
  
    }];
    
    [self layoutIfNeeded];
    _collectionView.frame = self.viewBackGround.bounds;
    
}

#pragma mark - ------------ cell赋值 ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    if (_arrSort.count != 0) {
        
        EnjoyModel *m = _arrSort[indexPath.row];
        cell.model = m;
    }
    
    return cell;
    
}

#pragma mark - ------------ 返回8个数 ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return 8;
    
}

#warning 1.1 Enjoy  通知中心POST  传出去下标

#pragma mark - ------------ item 通知中心 点击 判断点击哪个item ---------------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    /** 发通知 */
    [[NSNotificationCenter defaultCenter]postNotificationName:@"index" object:@(indexPath.item)];

}


#pragma mark - ------------ 头部cell ---------------

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *header = [collectionView  dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        self.labelHeader = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 10, header.bounds.size.width, 50)];
        _labelHeader.text = @"紛頛";
        _labelHeader.font = [UIFont systemFontOfSize:20];
        _labelHeader.textAlignment = NSTextAlignmentCenter;
     
        
        self.labelLine = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 35, header.bounds.size.width, 25)];
        _labelLine.text = @"______";
        _labelLine.textColor = [UIColor grayColor];
        _labelLine.textAlignment = NSTextAlignmentCenter;

        
        

        [header addSubview:_labelHeader];
        [header addSubview:_labelLine];
        
        [_labelHeader release];
        [_labelLine release];
        
        return header;
    }
    return nil;


}



#pragma mark - ------------ 重写 arr set 方法 collection 在这 reloadata ---------------
- (void)setArrSort:(NSArray *)arrSort
{

    if (_arrSort != arrSort) {
        
        [_arrSort release];
        _arrSort = [arrSort retain];
        
    }
    [_collectionView reloadData];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
